package GUI;

import 学生.Student;
import 数据库和网络.myDatebase;
import 试题和试卷.*;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.ArrayList;

public class revisePaperChooseWindow extends JFrame {

    String[] papername;
    int[] papertime;
    String tableName="";
    int paperNumber=0;
    JPanel mainPanel;
    int currentPage,page,size;
    JLabel num,paperLabel,pageNumberLabel;
    JButton detailButton,doButton,nextButton,preButton;
    ArrayList<revisePaperItem> paperItems = new ArrayList<revisePaperItem>();
    ArrayList<paper> papers;
    Font font = new Font("sanserif",Font.BOLD,10);
    Font pageNumberFont = new Font("sanserif",Font.BOLD,12);
    public static Student currentstudent;
    static revisePaperChooseWindow revisePaperChoose;

    public static void main(String[] args) {
        //revisePaperChoose =new revisePaperChooseWindow(currentStudent);

    }


    public revisePaperChooseWindow(Student currentStudent){
        currentstudent=currentStudent;
        myDatebase queryDateBase=new myDatebase("information_schema");
        myDatebase paperDateBase=new myDatebase("papercollection");
        try {
            String sql="select TABLE_NAME from information_schema.TABLES where information_schema.TABLES.TABLE_NAME='"+currentStudent.getMyClass()+"' ";
            queryDateBase.statement.execute(sql);
            queryDateBase.resultSet=queryDateBase.statement.executeQuery(sql);
            while (queryDateBase.resultSet.next()){
                tableName=queryDateBase.resultSet.getString(1);
                System.out.println(tableName);
            }/*上面是寻找到currentsutdent所在的班级*/

            String sql1="select TABLE_ROWS from TABLES where TABLE_NAME='"+tableName+"'";
            queryDateBase.statement.execute(sql1);
            queryDateBase.resultSet=queryDateBase.statement.executeQuery(sql1);
            while(queryDateBase.resultSet.next()){
                paperNumber=queryDateBase.resultSet.getInt(1);
            }/*上面是从TABLE表中确定试卷数，以上是为了通过currentStudent的班级信息连接上数据库自带的存储信息的TABLES表，从而找到
        对应班级的试卷数据库以及对应的试卷库信息*/

            String sql2=String.format("select * from papercollection.%s",tableName);
            System.out.println(paperNumber);
            papername=new String[paperNumber];
            papertime=new int[paperNumber];
            paperDateBase.statement.execute(sql2);
            paperDateBase.resultSet=paperDateBase.statement.executeQuery(sql2);
            int index=0;
            while(paperDateBase.resultSet.next()){
                papertime[index]=paperDateBase.resultSet.getInt(2);
                System.out.println(papertime[index]);
                papername[index]=paperDateBase.resultSet.getString(1);
                System.out.println(papername[index]);
                index++;
                /*这里是找到对应的班级数据库，从中找到paperTitle输出到String[]里，也就是papername，
                这样就可以再根据paper类中的方法把这些paper的数据加载到一个arraylist：papers中*/
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally{
            queryDateBase.close();
            paperDateBase.close();
        }

        papers=new ArrayList<>(paperNumber);
        for(int i=0;i<paperNumber;i++){
            papers.add(new paper());
        }


        for(int i=0;i<paperNumber;i++){
            papers.get(i).getQuestions(tableName,papername[i]);
            papers.get(i).setName(papername[i]);
            papers.get(i).setTime(papertime[i]);
        }/*将试卷list中每个试卷分别找到他们的questions,为下面打印出每个试卷的基本信息做准备，
        没使用迭代器是因为papername要使用下标*/



        mainPanel = new JPanel();
        JLabel infoLabel = new JLabel("试卷数目:"+paperNumber);
        JLabel numLabel = new JLabel("序号");
        JLabel paperInfoLabel = new JLabel("试卷信息");
        JLabel paperDetailLabel = new JLabel("批改");


        preButton = new JButton("上一页");
        preButton.addActionListener(new preListener());
        preButton.setBounds(670, 450, 70, 30);
        preButton.setFont(font);
        preButton.setBackground(Color.lightGray);

        setTitleType(numLabel);
        setTitleType(paperInfoLabel);
        setTitleType(paperDetailLabel);


        JButton backButton = new JButton("返回");

        mainPanel.setLayout(null);

        backButton.setBackground(Color.lightGray);

        infoLabel.setBounds(80,30,100,40);
        numLabel.setBounds(80,120,60,50);
        paperInfoLabel.setBounds(140,120,600,50);
        paperDetailLabel.setBounds(740,120,80,50);

        backButton.setBounds(740, 500, 80, 30);



        mainPanel.add(paperDetailLabel);

        mainPanel.add(numLabel);
        mainPanel.add(infoLabel);
        mainPanel.add(paperInfoLabel);
        mainPanel.add(backButton);


        size =paperNumber;

        page=(size-1)/5+1;
        currentPage=1;

        pageNumberLabel=new JLabel(currentPage+"/"+page);
        mainPanel.add(pageNumberLabel);
        pageNumberLabel.setFont(pageNumberFont);
        pageNumberLabel.setBounds(430,430,80,30);

        for (int i=1;i<=size;i++){
            paperItems.add(new revisePaperItem(i,papers.get(i-1)));
        }
        if (page==1){
            for(int i=1;i<=size;i++){
                add(paperItems.get(i-1));
            }
        }else if(page>1){
            for(int i= (currentPage-1)*5;i<=currentPage*5-1;i++){
                add(paperItems.get(i));
            }
            nextButton = new JButton("下一页");
            nextButton.addActionListener(new nextListener());
            nextButton.setBounds(750, 450, 70, 30);
            nextButton.setFont(font);
            nextButton.setBackground(Color.lightGray);
            mainPanel.add(nextButton);
        }

        backButton.addActionListener(new backListener());
        infoLabel.setHorizontalAlignment(SwingConstants.CENTER);
        infoLabel.setVerticalAlignment(SwingConstants.CENTER);
        this.setSize(900, 650);
        mainPanel.setSize(this.getSize());
        mainPanel.setBackground(Color.white);
        this.setBackground(Color.white);
        this.setContentPane(mainPanel);
        this.setLocationRelativeTo(null);//居中
        this.setTitle("学生在线考试平台");
        this.setVisible(true);
        //this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public void setTitleType(JLabel label){
        Font bigFont = new Font("sanserif",Font.PLAIN,16);
        label.setBackground(Color.lightGray);
        label.setOpaque(true);
        label.setFont(bigFont);
        label.setBorder(BorderFactory.createLineBorder(Color.darkGray));
        label.setHorizontalAlignment(SwingConstants.CENTER);
    }
    public void setTextType(JLabel label){
        Font smallFont = new Font("sanserif",Font.PLAIN,12);
        label.setBackground(Color.white);
        label.setOpaque(true);
        label.setFont(smallFont);
        label.setBorder(BorderFactory.createLineBorder(Color.lightGray));
        label.setHorizontalAlignment(SwingConstants.CENTER);
    }
    public void setButtonType(JButton button){
        Font smallFont = new Font("sanserif",Font.PLAIN,14);
        button.setBackground(Color.GRAY);
        button.setFont(smallFont);
        button.setBorder(BorderFactory.createLineBorder(Color.darkGray));
        button.setHorizontalAlignment(SwingConstants.CENTER);
    }
    public class backListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {//关闭注册界面

            revisePaperChooseWindow.this.dispose();
            new teacherWindow("a");
        }
    }
    public class nextListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {

            for(int i= (currentPage-1)*5;i<=currentPage*5-1;i++){
                remove(paperItems.get(i));
            }
            currentPage++;
            pageNumberLabel.setText(currentPage+"/"+page);
            if(currentPage!=1){
                mainPanel.add(preButton);}
            if(currentPage==page){
                mainPanel.remove(nextButton);
                for(int i= (currentPage-1)*5;i<size;i++){
                    add(paperItems.get(i));
                }
            }else{
                for(int i= (currentPage-1)*5;i<=(currentPage*5-1);i++){
                    add(paperItems.get(i));
                }
            }

            mainPanel.updateUI();

        }
    }
    public class preListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            for(int i=0;i<size;i++){
                remove(paperItems.get(i));
            }
            if(currentPage==2){
                mainPanel.remove(preButton);
            }
            mainPanel.updateUI();
            currentPage--;
            if(currentPage==1){
                mainPanel.remove(preButton);
            }
            pageNumberLabel.setText(currentPage+"/"+page);
            if(currentPage!=page){
                mainPanel.add(nextButton);
            }
            for(int i= (currentPage-1)*5;i<=currentPage*5;i++){
                add(paperItems.get(i));
            }

            if(currentPage<1){
                currentPage=1;
            }
            mainPanel.updateUI();
        }
    }
    public void add(revisePaperItem item){
        mainPanel.add(item.num);
        mainPanel.add(item.paperLabel);
        mainPanel.add(item.reviseButton);
    }
    public void remove(revisePaperItem item){
        mainPanel.remove(item.num);
        mainPanel.remove(item.paperLabel);
        mainPanel.remove(item.reviseButton);

    }}